package com.beetlestech.nfe.main.biz.com.impl;

import java.sql.Date;
import java.sql.Time;
import java.util.ArrayList;
import java.util.List;

import com.beetlestech.nfe.fw.core.biz.AbstractDBLogic;
import com.beetlestech.nfe.main.biz.com.HbAccesslog;
import com.beetlestech.nfe.main.biz.constant.CommonConstant;
import com.beetlestech.nfe.main.biz.constant.CtgCodeConstant;
import com.beetlestech.nfe.main.biz.constant.HbMessageCode;
import com.beetlestech.nfe.main.db.tbl.TacclogDTO;

/**
 * アクセスログ情報 Bizロジックの実装クラス
 *
 * 変更履歴
 * REV.         変更日付           変更者              変更内容
 * -------      ---------------    ----------------    ------------------
 * 1.0          2013/06/11         GD 王勇             新規作成
 * 1.1			2013/08/22		   GD 馬			   変更53対応
 * 1.2			2014/05/03		   GD 王昕			   変更228対応
 */
public class HbAccesslogImpl extends AbstractDBLogic implements HbAccesslog, CtgCodeConstant, HbMessageCode, CommonConstant {

	/* (non-Javadoc)
	 * @see com.beetlestech.nfe.main.biz.com.HbAccesslog#createAccessLog(java.lang.String, java.lang.String)
	 */
	@Override
	public Boolean createAccessLog(
			int carId,
			String accessInfo) {
		//登録値をセットする
		Date currentDate = getCurrentDate();
		Time currentTime = getCurrentTime();
		TacclogDTO tacclogDTO = TacclogDTO.setDefaultValue();
		tacclogDTO.setCarID(carId);
		tacclogDTO.setEmployeeID(Integer.parseInt(getUserId()));
		tacclogDTO.setAccStDate(currentDate);
		tacclogDTO.setAccStTime(currentTime);
		tacclogDTO.setAccWsNo(getWsNo());
		tacclogDTO.setAccEdDate(currentDate);
		tacclogDTO.setAccEdTime(currentTime);
		tacclogDTO.setAccStatus(TACCLOG_ACC_STATUS.ACC_STATUS_0.toString());
		tacclogDTO.setAccess(accessInfo);
		tacclogDTO.setYobiFld(getIpAddress());

		TacclogDTO resultDTO = updateDAO.insertOneImmediatelyByCheckKey(TacclogDTO.INSERT, tacclogDTO, M10430WS);

		if (resultDTO != null) {
			boolean sameflg = false;
			int countsame = 0;
			String[] accessInfoArray = accessInfo.split(COMMON_COMMA);
			List<String> accessInfos = new ArrayList<String>();
			String accessInfoStr = resultDTO.getAccess();
			for (int i = 0; i < accessInfoArray.length; i++) {
				if (accessInfoStr.indexOf(accessInfoArray[i]) < 0) {
					accessInfos.add(accessInfoArray[i]);
				} else {
					countsame++;
				}
				// 完全に同じ
				if (countsame == accessInfoArray.length) {
					sameflg = true;
				}
			}
			if (!sameflg) {
				StringBuilder newAccessInfo = new StringBuilder();
				newAccessInfo.append(accessInfoStr);
				newAccessInfo.append(COMMON_COMMA);
				for (int i = 0; i < accessInfos.size(); i++) {
					newAccessInfo.append(accessInfos.get(i));
					newAccessInfo.append(COMMON_COMMA);
				}
				// 最後の ","を削除する
				newAccessInfo.deleteCharAt(newAccessInfo.length() - 1);
				// TACCLOGを以下の条件でUPDATEする
				TacclogDTO tacclogDTOupdate = new TacclogDTO();
				tacclogDTOupdate.setCarID(carId);
				tacclogDTOupdate.setEmployeeID(Integer.parseInt(getUserId()));
				tacclogDTOupdate.setAccStDate(currentDate);
				tacclogDTOupdate.setAccStTime(currentTime);
				tacclogDTOupdate.setAccWsNo(getWsNo());
				tacclogDTOupdate.setAccess(newAccessInfo.toString());
				updateDAO.updateOneImmediately(TacclogDTO.UPDATE, tacclogDTOupdate, M10430WS);
			}
		}
		return true;
	}
}
